package org.csu.myblogger.myblogger.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.http.Header;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.commom.JwtTokenUtil;
import org.csu.myblogger.myblogger.entity.Article;
import org.csu.myblogger.myblogger.entity.Comment;
import org.csu.myblogger.myblogger.entity.WaitComment;
import org.csu.myblogger.myblogger.service.service.AccountService;
import org.csu.myblogger.myblogger.service.service.ArticleService;
import org.csu.myblogger.myblogger.service.service.CommentService;
import org.csu.myblogger.myblogger.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment/")
public class CommentController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @PutMapping("PublishComment/{username}")
    public CommonResponce PublishComment(@PathVariable String username,
                                         @RequestBody CommentVo commentVo,
                                         @RequestHeader ("Authorization") String token){//使用vo封装

        try{
            String testname=jwtTokenUtil.getUsernameFromToken(token);
            if (!testname.equals(username)){
                return CommonResponce.createForError(1,"无权操作当前该用户");
            }
            return commentService.PublishComment(username,commentVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token异常");
        }
    }
    @DeleteMapping("DeleteComment/{username}")
    public CommonResponce DeleteComment(@PathVariable String username,@RequestBody CommentVo commentVo,
                                        @RequestHeader("Authorization") String token){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(token);
            if (!testname.equals(username)){
                return CommonResponce.createForError(1,"无权操作当前该用户");
            }
            return commentService.DeleteComment(username,commentVo);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token异常");
        }
    }
    //展示待审核评论
    @GetMapping("AuditComment/{username}")
    public CommonResponce<List<WaitComment>> AuditComment(@PathVariable String username,
                                                          @RequestHeader("Authorization") String token){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(token);
            if (!testname.equals(username)){
                return CommonResponce.createForError(1,"无权操作当前该用户");
            }
            return commentService.AuditComment(username);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token异常");
        }
    }
    @PostMapping("UpdateCommemt/{username}")
    public CommonResponce UpdateCommemt(@PathVariable String username,@RequestBody List<Comment> comments,
                                       @RequestHeader("Authorization") String token){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(token);
            if (!testname.equals(username)){
                return CommonResponce.createForError(1,"无权操作当前该用户");
            }
            return commentService.UpdateCommemt(username,comments);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token异常");
        }
    }
}
